package com.navinfo.opentsp.platform.computing.analysis.entity;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.navinfo.opentsp.platform.computing.analysis.util.DataUtil;
import com.navinfo.stream.qingqi.protocol.java.LCVehicleBreakdown;
import org.apache.spark.sql.Row;
import scala.collection.mutable.WrappedArray;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class BreakdownAdditionDto {

    private int breakdownSPNValue;
    private int breakdownFMIValue;
    private int spnChangeMode;
    private int faultsCount;
    private int faultType;

    public BreakdownAdditionDto() {}

    public BreakdownAdditionDto(int breakdownFMIValue, int breakdownSPNValue, int faultsCount, int faultType, int spnChangeMode) {
        this.breakdownSPNValue = breakdownSPNValue;
        this.breakdownFMIValue = breakdownFMIValue;
        this.spnChangeMode = spnChangeMode;
        this.faultsCount = faultsCount;
        this.faultType = faultType;
    }

    public int getBreakdownSPNValue() {
        return breakdownSPNValue;
    }

    public void setBreakdownSPNValue(int breakdownSPNValue) {
        this.breakdownSPNValue = breakdownSPNValue;
    }

    public int getBreakdownFMIValue() {
        return breakdownFMIValue;
    }

    public void setBreakdownFMIValue(int breakdownFMIValue) {
        this.breakdownFMIValue = breakdownFMIValue;
    }

    public int getSpnChangeMode() {
        return spnChangeMode;
    }

    public void setSpnChangeMode(int spnChangeMode) {
        this.spnChangeMode = spnChangeMode;
    }

    public int getFaultsCount() {
        return faultsCount;
    }

    public void setFaultsCount(int faultsCount) {
        this.faultsCount = faultsCount;
    }

    public int getFaultType() {
        return faultType;
    }

    public void setFaultType(int faultType) {
        this.faultType = faultType;
    }

    public static BreakdownAdditionDto parseProto(LCVehicleBreakdown.VehicleBreakdown vehicleBreakdown){
        BreakdownAdditionDto breakdownAdditionDto = new BreakdownAdditionDto();
        breakdownAdditionDto.setBreakdownFMIValue(vehicleBreakdown.getBreakdownFMIValue());
        breakdownAdditionDto.setBreakdownSPNValue(vehicleBreakdown.getBreakdownSPNValue());
        breakdownAdditionDto.setFaultsCount(vehicleBreakdown.getFaultsCount());
        breakdownAdditionDto.setFaultType(vehicleBreakdown.getFaultType());
        breakdownAdditionDto.setSpnChangeMode(vehicleBreakdown.getSpnChangeMode());
        return breakdownAdditionDto;
    }

    public static BreakdownAdditionDto parseString(String obj){
        String[] arr = obj.split("_");
        return new BreakdownAdditionDto(
                Integer.parseInt(arr[0]),
                Integer.parseInt(arr[1]),
                Integer.parseInt(arr[2]),
                Integer.parseInt(arr[3]),
                Integer.parseInt(arr[4])
        );
    }

    public static List<BreakdownAdditionDto> parseToList(String breakdownlist){
        if(breakdownlist == null){
            return Collections.emptyList();
        }
        TypeReference<List<String>> typeReference = new TypeReference<List<String>>(){};
        List<String> dataList = JSON.parseObject(breakdownlist, typeReference);
        List<BreakdownAdditionDto> statusList = new ArrayList<>();
        for(String data : dataList){
            statusList.add(parseString(data));
        }
        return statusList;
    }

    public static List<BreakdownAdditionDto> parseRow(Row structRow){
        if(structRow == null){
            return Collections.emptyList();
        }
        List<BreakdownAdditionDto> breakdownArrayList = new ArrayList<>();
        WrappedArray.ofRef array = structRow.getAs("breakdown");
        if(array == null){
            return Collections.emptyList();
        }
        scala.collection.Iterator<Row> itor = array.iterator();
        while(itor.hasNext()){
            Row row = itor.next();
            int breakdownSPNValue = DataUtil.getValue(row.getAs("breakdownSPNValue"), 0);
            int breakdownFMIValue = DataUtil.getValue(row.getAs("breakdownFMIValue"), 0);
            int spnChangeMode = DataUtil.getValue(row.getAs("spnChangeMode"), 0);
            int faultsCount = DataUtil.getValue(row.getAs("faultsCount"), 0);
            int faultType = DataUtil.getValue(row.getAs("faultType"), 0);
            breakdownArrayList.add(new BreakdownAdditionDto(breakdownSPNValue, breakdownFMIValue, spnChangeMode, faultsCount, faultType));
        }
        return breakdownArrayList;
    }

}
